import { Player } from '../compoment/player';
import { AchiveItemId, BuildType, MapUnitType } from '../define/GameDefine';
import { UICommandKey } from '../define/UICommand';
import ViewMgr from '../manager/ViewMgr';
import RandomTool from '../tool/RandomTool';
import { Item } from './Item';
import { ItemContainer } from './ItemContainer';

export class MapUnit {
  /**
   *
   * @param i 对应x 从下往上
   * @param j 对应y 从左往右
   * ix:2 iy = 0 就是倒数第3行的第一个
   */
  private ix: number; //第几行 从下往上数
  public iy: number; //第几列 从左往右数
  public items = [];

  public constructor(i: number, j: number) {
    this.ix = i;
    this.iy = j;
    this.unitType = MapUnitType.Nothing;
    this.itemContain = new ItemContainer();
  }

  /**
   * 设置类型
   */
  public setType(type: number, buildCom: number): void {
    /**
     * 伪造一个随机类型
     */
    //this.unitType = 1 + Math.floor(RandomTool.getRandom() * 3);
    this.unitType = type;
    this.buildCom = buildCom;
    this.buildState = 2;
    this.propInit();
  }

  public buildState: number; //2是开启 1是关闭
  public maxHp: number;
  public hp: number = 0;
  private propInit(): void {
    switch (this.unitType) {
      case MapUnitType.Nothing:
        this.hp = 0;
        this.maxHp = 0;
        break;
      case MapUnitType.LuZhang:
        this.hp = 4;
        this.maxHp = 4;
        break;
      case MapUnitType.Stone:
        this.hp = 5;
        this.maxHp = 5;
        break;
      case MapUnitType.Tree:
        this.hp = 3;
        this.maxHp = 3;
        break;
    }
  }

  //地图的 基本显示类型
  private _unitType: MapUnitType;
  public buildCom: number;

  public get unitType(): MapUnitType {
    return this._unitType;
  }

  public set unitType(type: MapUnitType) {
    this._unitType = type;
  }

  //道具list
  public itemContain: ItemContainer;

  //检测人能不能走上去
  public checkRun(): boolean {
    if (this.unitType == MapUnitType.Nothing) return true;
    return false;
  }

  //当被玩家交互
  public onDeal(player: Player): void {
    if (this._unitType == MapUnitType.Nothing) {
      return;
    }
    let x: number = this.ix;
    let y: number = this.iy;
    if (this.buildCom && this.buildCom > 1) {
      switch (this.buildCom) {
        case 2:
          x = x - 1;
          break;
        case 3:
          x = x - 1;
          y = y - 1;
          break;
        case 4:
          y = y - 1;
          break;
      }
      ViewMgr.ins.emit(UICommandKey.buildDealEvent, { hid: player.hid, x: x, y: y });
      return;
    }
    if (this.buildCom == 1) {
      //建筑物
      if (this.buildState == 1) return; //熄灭了
      let addIndex: number = player.getBagEmptySlot();
      if (addIndex == null) {
        ViewMgr.ins.hint('背包已满');
        return;
      }

      const id = this.getItemIdByBuildId(<number>this.unitType);
      if (!id) {
        console.log(`找不到 ${this.unitType}`);
        return;
      }
      console.log(`player: ${player.hid} pick item id: ${id}`);
      let item: Item = new Item(id);
      player.pickItem(item, addIndex);
      this.buildState = 1;
    } else {
      this.hp--;
      if (this.hp <= 0) this.onDel(player);
    }
    ViewMgr.ins.emit(UICommandKey.mapUnitChange, { x: x, y: y });
  }

  //被铲除
  public onDel(player:Player): void {
    this._unitType = MapUnitType.Nothing;
    //概率获得道具
    let ran:number = RandomTool.getRandom();
    if(ran<0.2){
      //获得道具
      let addIndex: number = player.getBagEmptySlot();
      if (addIndex == null) {
        ViewMgr.ins.hint('背包已满');
        return;
      }else{
        let ranPool:AchiveItemId[] = [AchiveItemId.Steal];
        let ranInd:number = Math.floor(ranPool.length*RandomTool.getRandom());
        let item: Item = new Item(ranPool[ranInd]);
        player.pickItem(item, addIndex);
      }
    }
    //ViewMgr.ins.emit(UICommandKey.mapUnitChange, { x: this.ix, y: this.iy });
  }

  public throwItem(item: Item) {
    this.items.push(item);
    console.log(this.items);
    ViewMgr.ins.emit(UICommandKey.mapUnitChange, { x: this.ix, y: this.iy });
  }

  public pickItem() {
    const item = this.items.pop();
    ViewMgr.ins.emit(UICommandKey.mapUnitChange, { x: this.ix, y: this.iy });
    return item;
  }

  public getItemIdByBuildId(buildId: BuildType): AchiveItemId {
    switch (buildId) {
      case BuildType.FrutShop:
        return AchiveItemId.Apple;
      case BuildType.Hospital:
        return AchiveItemId.Medic;
      case BuildType.EXRoom:
        return AchiveItemId.YaLing;
      case BuildType.Shop_Eye:
        return AchiveItemId.YanZhao;
      case BuildType.Shop_Clock:
        return AchiveItemId.Clock;
      case BuildType.PostBox:
        return AchiveItemId.Offer;
      case BuildType.Shop_Mac:
        return AchiveItemId.MacBook;
      case BuildType.OfficeBuild:
        return AchiveItemId.Card;
      case BuildType.Pauper:
        return AchiveItemId.Heart;
    }
  }
}
